package br.ufpe.cin.mouse.pizza.model;

import java.util.ArrayList;
import java.util.Date;

import br.ufpe.cin.mouse.pizza.Status;
import br.ufpe.cin.mouse.pizza.Util;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Transaction;

/**
 * This class defines the methods for basic operations of create, update &
 * retrieve for order entity
 * 
 * @author
 * 
 */
public class Order {

	public static String createOrUpdateOrder(String nomeCliente, Status status,
			Date data, ArrayList<LinhaItem> linhas) {

		Transaction txn = Util.getDatastoreServiceInstance().beginTransaction();
		try {

			// Pedido

			Entity order = new Entity("Order");
			order.setProperty("customerName", nomeCliente);
			order.setProperty("status", status.toString());
			order.setProperty("data", data);
			Util.getDatastoreServiceInstance().put(order);

			// Itens do Pedido

			for (LinhaItem linhaItem : linhas) {
				Entity lineItem = new Entity("LineItem", order.getKey());
				// key to string can be inserted instead of name, a better
				// option
				lineItem.setProperty("itemName", linhaItem.getItem());
				lineItem.setProperty("product", linhaItem.getProduct());
				lineItem.setProperty("quantity", linhaItem.getQuantidade());
				lineItem.setProperty("price", linhaItem.getValor());
				lineItem.setProperty("total", linhaItem.getTotal());
				lineItem.setProperty("orderID",
						String.valueOf(order.getKey().getId()));
				Util.getDatastoreServiceInstance().put(lineItem);
			}

			txn.commit();

			return String.valueOf(order.getKey().getId());

		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
		}

	}

	public static String atualizarStatus(String orderId) {

		Transaction txn = Util.getDatastoreServiceInstance().beginTransaction();
		try {

			// Pedido

			Entity order = obterPedido(orderId);
			order.setProperty("status", Status.ATENDIDO.toString());
			Util.getDatastoreServiceInstance().put(order);

			txn.commit();

			return String.valueOf(order.getKey().getId());

		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
		}

	}

	/**
	 * Get all the orders
	 * 
	 * @return : list of all orders
	 */
	public static Iterable<Entity> getAllOrders() {
		Iterable<Entity> entities = Util.listEntities("Order", null, null);
		return entities;
	}

	/**
	 * Get all the orders
	 * 
	 * @return : list of all orders
	 */
	public static Iterable<Entity> getLineItens(long orderID) {

		Iterable<Entity> entities = Util.listEntities("LineItem", "orderID", ""
				+ orderID);
		return entities;
	}

	/**
	 * Get the list of orders for a specific customer
	 * 
	 * @param customerName
	 * @return the list of orders as iterable
	 */
	public static Iterable<Entity> getAllOrdersForCustomer(String customerName) {
		Key customerKey = KeyFactory.createKey("Customer", customerName);
		return Util.listChildren("Order", customerKey);
	}

	public static Entity getOrder(String orderId) {
		Key key = KeyFactory.createKey("Order", orderId);
		return Util.findEntity(key);
	}

	public static Entity obterPedido(String orderId) {

		Iterable<Entity> entities = getAllOrders();

		for (Entity entity : entities) {
			if (Long.parseLong(orderId) == entity.getKey().getId()) {
				return entity;
			}
		}

		return null;
	}

}
